package com.qf.pickname.controller;

import com.qf.pickname.entity.Student;
import com.qf.pickname.ex.StudentException;
import com.qf.pickname.service.StudentService;
import com.qf.pickname.utils.StudentDataStore;
import com.qf.pickname.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@CrossOrigin  // 后端处理跨域请求
@RestController
public class StudentController {

    // 需要通过spring注解实现下面属性的赋值操作
    @Autowired  // 自动配置
    private StudentService studentService;

    @RequestMapping("/picknamepro")
    public ResultVo picknamepro(){

        ResultVo resultVo = new ResultVo();
        Student student = studentService.randomStudentPlus();
//        Student student = studentService.randomStudentProMax();
        if (student!=null){
            resultVo.setCode(1);
            resultVo.setMsg("ok");
            resultVo.setData(student);
        }else {
            resultVo.setCode(-1);
            resultVo.setMsg("都点完了");
        }

        return resultVo;

    }


    // http://localhost:9999/add?name=名字&sex=性别
    @RequestMapping("/add")
    public ResultVo addStudent(String name, String sex) {

        if (name == null || "".equals(name)) {
//            return new ResultVo(-100, "学生的名字不能为空");
            StudentException studentException = new StudentException();
            studentException.setStuExCode(-100);
            studentException.setStuExMsg("学生名字为空");
            studentException.setStuExDetail("添加学生,学生的名字为空");
            throw studentException;
        }
        if (sex == null || "".equals(sex)) {
            return new ResultVo(-100, "学生的性别不能为空");
        }

        // 调用service层的对象的方法，进行具体的业务逻辑处理
        Boolean result = studentService.addStudent(name, sex);
        if (result) {
            return new ResultVo(1, "ok");
        } else {
            return new ResultVo(-1, "添加学生失败");
        }

    }

    @RequestMapping("/pickname")
    public Student pickname() {

        Student student = studentService.randomStudent();

        return student;
    }

}
